<?php

namespace app\admin\logic;

use  app\admin\error\Login as LoginError;
use app\api\controller\Token;
use app\common\service\TokenService;

/**
 * 登录逻辑层
 */
class LoginLogic extends AdminBase
{
    /**
     * 示例
     */
    public static function index(array $params = []): array
    {
        //1.验证参数
        self::paramSceneCheck(self::validate("Login"), "login", $params);
        $token = new TokenService(['type' => 'ad', 'conValue' => 'cs']);
        $auth = ['user_name' => $params['username'], 'password' => $params['password']];
        return $token->createAccessToken($auth);
    }

    /**
     * 示例
     */
    public static function detail(array $params = []): array
    {
        $admin = Oauth::$userInfo;
        unset($admin['id']);
        return returnData($admin);
    }

    /**
     * 获取router
     * @param array $params
     * @return array
     */
    public static function getRouter(array $params = []): array
    {
        $roles = '{"code":200,"data": [{
          "_id": "5fd97ee46decb9617960fb9b",
          "hidden": false,
          "permissions": "",
          "sort": 1,
          "pid": "0",
          "type": "1",
          "title": "系统管理",
          "path": "/system",
          "component": "Layout",
          "date": "2020-12-16T03:28:36.891Z",
          "__v": 0,
          "alwaysShow": true,
          "redirect": "noRedirect",
          "system": "1",
          "meta": { "title": "系统管理", "icon": "el-icon-s-grid", "roles": [], "noCache": true },
          "children": [{
            "_id": "5fd97f236decb9617960fb9c",
            "hidden": false,
            "permissions": "",
            "sort": 1,
            "pid": "5fd97ee46decb9617960fb9b",
            "type": "2",
            "title": "菜单管理",
            "path": "/system/menu",
            "component": "system/menu/index",
            "name": "menu",
            "date": "2020-12-16T03:29:39.426Z",
            "__v": 0,
            "system": "1",
            "meta": { "title": "菜单管理", "icon": "el-icon-menu", "roles": ["add"], "noCache": true },
            "hasChildren": 0
          }, {
            "_id": "6035f0a953c7055819d0cbf3",
            "hidden": false,
            "permissions": "",
            "sort": 0,
            "pid": "5fd97ee46decb9617960fb9b",
            "type": "2",
            "title": "角色管理",
            "path": "/system/roles",
            "component": "system/roles/index",
            "name": "roles",
            "date": "2021-02-24T06:22:33.366Z",
            "__v": 0,
            "system": 1,
            "meta": { "title": "角色管理", "icon": "el-icon-warning", "roles": ["add"], "noCache": true },
            "hasChildren": 0
          }, {
            "_id": "605d40ba4a71f834e2ca0a93",
            "hidden": false,
            "permissions": "",
            "sort": 0,
            "pid": "5fd97ee46decb9617960fb9b",
            "type": "2",
            "title": "用户管理",
            "path": "/system/user",
            "component": "system/user/index",
            "name": "user",
            "date": "2021-03-26T02:02:34.367Z",
            "__v": 0,
            "system": 1,
            "meta": { "title": "用户管理", "icon": "el-icon-warning", "roles": ["add"], "noCache": true },
            "hasChildren": 0
          }],
          "hasChildren": 1
        }, {
          "_id": "607da497bc2f2f1ca6819d95",
          "hidden": false,
          "permissions": "",
          "sort": 3,
          "pid": "0",
          "type": "1",
          "title": "富文本展示",
          "path": "/richText",
          "component": "Layout",
          "redirect": "noRedirect",
          "alwaysShow": true,
          "date": "2021-01-03T10:44:36.418Z",
          "__v": 0,
          "system": "1",
          "meta": { "title": "富文本展示", "icon": "el-icon-warning", "roles": [], "noCache": true },
          "children": [{
            "_id": "607da57fbc2f2f1ca6819d96",
            "hidden": false,
            "permissions": "",
            "sort": 0,
            "pid": "607da497bc2f2f1ca6819d95",
            "type": "2",
            "title": "tiptap",
            "path": "/richText/tiptap",
            "component": "richText/tiptap/index",
            "name": "ue",
            "date": "2021-03-29T03:20:12.746Z",
            "__v": 0,
            "system": 1,
            "meta": { "title": "tiptap", "icon": "el-icon-warning", "roles": [], "noCache": true }
          }],
          "hasChildren": 1
        }, {
          "_id": "test1",
          "hidden": false, "permissions": "",
          "sort": 3,
          "pid": "0",
          "type": "1",
          "title": "富文本展示",
          "path": "/baseList",
          "component": "Layout",
          "redirect": "noRedirect",
          "alwaysShow": true,
          "date": "2021-01-03T10:44:36.418Z",
          "__v": 0,
          "system": "1",
          "meta": { "title": "列表组件", "icon": "el-icon-warning", "roles": [], "noCache": true },
          "children": [{
            "_id": "6061476c4a71f834e2ca0a96",
            "hidden": false,
            "permissions": "",
            "sort": 0,
            "pid": "5ff1a01455d63d1b66560284",
            "type": "2",
            "title": "base-list",
            "path": "/baseList/show",
            "component": "baseList/show/index",
            "name": "ue",
            "date": "2021-03-29T03:20:12.746Z",
            "__v": 0,
            "system": 1,
            "meta": { "title": "base-list", "icon": "el-icon-warning", "roles": [], "noCache": true }
          }],
          "hasChildren": 1
        }],"message":"获取路由成功"}';
        return returnData([
            "username" => "admin",
            "avatar"   => "123456",
            "roles"    => json_decode($roles, true)["data"],
        ]);
    }
}
